package com.lagou.serializer;

import com.alibaba.fastjson.JSON;

import java.io.IOException;

/**
 * JSONSerializer
 *
 * @author zyt
 * @since 2020/8/12 22:50
 */
public class JSONSerializer implements Serializer{
    /**
     * java对象转换为二进制
     *
     * @param object
     * @return
     */
    public byte[] serialize(Object object) throws IOException {
        return JSON.toJSONBytes(object);
    }

    /**
     * 二进制转换成java对象
     *
     * @param clazz
     * @param bytes
     * @return
     */
    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws IOException {
        return JSON.parseObject(bytes, clazz);
    }
}
